package action

import (
	"MessageSendService/app/api/internal/process"
	"MessageSendService/app/common/log"
	"MessageSendService/app/common/models"
	"errors"
)

type PreParamAction struct {
}

func NewPreParamAction() *PreParamAction {
	return &PreParamAction{}
}

// 前置检查,主要检查参数是否合法，消息参数是否存在，
func (p *PreParamAction) Process(ctx *process.ProcessContext) error {
	log.GetLogger().Info("开始前置检查.....")
	if ctx.ProcessModel.MessageParam == nil || len(ctx.ProcessModel.MessageParam) == 0 {
		return errors.New("消息参数不能为空")
	}
	var messageParam []models.MessageParam
	// 过滤 receiver=null 的messageParam
	for i := 0; i < len(ctx.ProcessModel.MessageParam); i++ {
		if ctx.ProcessModel.MessageParam[i].Receiver != "" {
			messageParam = append(messageParam, ctx.ProcessModel.MessageParam[i])
		}
	}
	if len(messageParam) <= 0 {
		return errors.New("过滤后结果参数为空")
	}
	return nil
}
